package org.libarae.communication.common;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class Message extends Common {
	private int mMessageId;
	private int mThreadId;
	private String mMessageText;
	private int mToUser;
	private boolean mIsRead;
	private long mTime;
	private boolean mNeedsApproval = false;

	//main constructor
	public Message() {
		super();
	}

	public Message(int messageId, String messageText, int toUser, int threadId, boolean isRead, long time, boolean needApproval) {
		super();
		setMessageId(messageId);
		setMessageText(messageText);
		setThreadId(threadId);
		setToUser(toUser);
		setIsRead(isRead);
		setTime(time);
		setNeedsApproval(needApproval);
	}

	@Override
	public void decodeXML(Node root) {
		NodeList nodeList = root.getChildNodes();
		
		// set all of the nodes in their proper places
		for (int i = 0; i < nodeList.getLength(); i++) {
			// find what node we have
			String nodeName = nodeList.item(i).getNodeName();
			
			if (nodeName.equals("mMessageId")) {
				mMessageId = Integer.parseInt(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mThreadId")) {
				mThreadId = Integer.parseInt(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mMessageText")) {
				mMessageText = nodeList.item(i).getTextContent();
			}
			else if (nodeName.equals("mToUser")) {
				mToUser = Integer.parseInt(nodeList.item(i).getTextContent());
			}
			else if (nodeName.equals("mTime")) {
				mTime = Long.parseLong(nodeList.item(i).getTextContent());
			}					
			else if (nodeName.equals("mIsRead")) {
				if(Integer.parseInt(nodeList.item(i).getTextContent()) == 1)
					mIsRead = true;
				else
					mIsRead = false;
			}
			else if (nodeName.equals("mNeedsApproval")) {
				if(Integer.parseInt(nodeList.item(i).getTextContent()) == 1)
					mNeedsApproval = true;
				else
					mNeedsApproval = false;
			}	
			else {
				System.err.println("Unknow field in object XML: " + nodeName);
			}
		}		
	}
	
	
	@Override
	public void createXml(Document doc, Element root) {
		Element node = doc.createElement("mMessageId");
		node.setTextContent(mMessageId+"");
		root.appendChild(node);
		
		node = doc.createElement("mThreadId");
		node.setTextContent(mThreadId+"");
		root.appendChild(node);
		
		node = doc.createElement("mMessageText");
		node.setTextContent(mMessageText);
		root.appendChild(node);
		
		node = doc.createElement("mTime");
		node.setTextContent(mTime+"");
		root.appendChild(node);	
		
		node = doc.createElement("mToUser");
		node.setTextContent(mToUser+"");
		root.appendChild(node);	
		
		node = doc.createElement("mIsRead");
		if(mIsRead)
			node.setTextContent("1");
		else
			node.setTextContent("0");
		root.appendChild(node);
		
		node = doc.createElement("mNeedsApproval");
		if(mNeedsApproval)
			node.setTextContent("1");
		else
			node.setTextContent("0");
		root.appendChild(node);
	}

	public int getMessageId() {
		return mMessageId;
	}

	public void setMessageId(int mMessageId) {
		this.mMessageId = mMessageId;
	}

	public int getThreadId() {
		return mThreadId;
	}

	public void setThreadId(int mThreadId) {
		this.mThreadId = mThreadId;
	}

	public String getMessageText() {
		return mMessageText;
	}

	public void setMessageText(String mMessageText) {
		if(mMessageText == null)
			this.mMessageText = "";
		else
			this.mMessageText = mMessageText;
	}

	public int getToUser() {
		return mToUser;
	}

	public void setToUser(int mToUser) {
		this.mToUser = mToUser;
	}

	public boolean isIsRead() {
		return mIsRead;
	}

	public void setIsRead(boolean mIsRead) {
		this.mIsRead = mIsRead;
	}

	public long getTime() {
		return mTime;
	}

	public void setTime(long mTime) {
		this.mTime = mTime;
	}
	
	public boolean isNeedsApproval() {
		return mNeedsApproval;
	}

	public void setNeedsApproval(boolean mNeedsApproval) {
		this.mNeedsApproval = mNeedsApproval;
	}
}
